{include file="public/header" /}

<style>
    ul>li {
        width: auto !important;
    }
</style>
<div class="page">
    <div class="fixed-empty"></div>
    <form id="question_class_form" method="post" name="articleClassForm">
        <table class="ds-default-table">
            <tbody>
                <input type="hidden" name="question_id" value="{$model->id | default = 0}">
                <tr>
                    <td colspan="2" class="required"><label class="validation">设备名称:</label>
                        <input type="text" value="" name="goods_name" id="gc_name"
                            style="width: 200px; margin-left: 60px;" class="txt"><span></span></td>
                    <td class="vatop tips"></td>
                </tr>
                <tr>
                    <td colspan="2" class="required"><label class="validation">商品分类:</label>
                        <div id="gcategory" style="margin-left: 134px;margin-top:-20px;width: 447px;">
                            <input type="hidden" name="class_id" value="{$type.class_id}" class="mls_id"
                                id="class_id" />
                            <input type="hidden" name="class_name" value="{$type.class_name|default=''}"
                                class="mls_name" />
                            {if $type.class_id}
                            <span>{$type.class_name|default=''}</span>
                            <input type="button" value="{$Think.lang.ds_edit}" class="edit_gcategory" />
                            {/if}
                            <select {if $type.class_id}style="display:none" {/if}>
                                <option value="0">请选择</option>
                                {foreach name="gc_list" item="gc"}
                                <option value="{$gc.gc_id}">{$gc.gc_name}</option>
                                {/foreach}
                            </select>
                        </div>
                    </td>
                </tr>
                <tr class="noborder" id="video_type">
                    <td class="required w120" style="width:50px;">设备图片:</td>
                    <td class="vatop rowform">
                        <div class="dssc-goods-default-pic">
                            <div class="goodspic-uplaod">
                                <span></span>
                                <input type="hidden" name="videos" id="video_name">

                                <input type="file" hidefocus="true" name="video" id="videos"
                                    data-action="{:url('Goodproject/ajax_videos')}">
                                </span>
                                <p class="hint"></p>
                            </div>
                        </div>
                        <div id="dataLoading" class="wp_data_loading">
                            <div class="data_loading"></div>
                        </div>
                        <div id="demo">
                        </div>
                    </td>
                </tr>

                <tr>
                    <td colspan="1" style="width: 70px;"><label>状态:</label>
                    <td class="onoff">
                        <label style="margin-left: 39px;" for="brand_recommend1"
                            class="cb-enable {if $brand_array.xianshi_state == '1'}selected{/if}"
                            title="{$Think.lang.ds_yes}"><span>启用</span></label>
                        <label for="brand_recommend0"
                            class="cb-disable {if $brand_array.xianshi_state == '2'}selected{/if}"
                            title="{$Think.lang.ds_no}"><span>禁用</span></label>
                        <input id="brand_recommend1" name="status" {if $brand_array.xianshi_state=='1'
                            }checked="checked" {/if} value="1" type="radio">
                        <input id="brand_recommend0" name="status" {if $brand_array.xianshi_state=='2'
                            }checked="checked" {/if} value="2" type="radio"></td>
                    </td>
                </tr>
            </tbody>
            <tfoot>
                <tr class="tfoot">
                    <td colspan="15">
                        <input class="btn" id="btn" type="button" value="{$Think.lang.ds_submit}" />
                    </td>
                </tr>
            </tfoot>
        </table>
    </form>
</div>
<link rel="stylesheet" href="{$Think.PLUGINS_SITE_ROOT}/js/jquery-ui-timepicker/jquery-ui-timepicker-addon.min.css">
<script src="{$Think.PLUGINS_SITE_ROOT}/js/jquery-ui-timepicker/jquery-ui-timepicker-addon.min.js"></script>
<script src="{$Think.PLUGINS_SITE_ROOT}/js/jquery-ui-timepicker/i18n/jquery-ui-timepicker-zh-CN.js"></script>
<script type="text/javascript" src="{$Think.PLUGINS_SITE_ROOT}/mlselection.js" charset="utf-8"></script>
<script type="text/javascript" src="{$Think.PLUGINS_SITE_ROOT}/jquery.mousewheel.js"></script>
<script>
    //增加一行
    // $("#add-science").click(function() {
    //       var str = '<tr>';
    //           str += '<td colspan="2" class="required"><label style="margin-left: 46px;"></label>';
    //           str += '<input type="text" value="" name="science[]" style="width: 200px; margin-left: 75px;" class="txt"><span></span>';
    //           str += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
    //           str += '<button id="add-science" type="button" class=" btn btn-small layui-btn-danger shanchu" style="background-color: #FF5722;">删除</button></td>';
    //           str +=  '</tr>';
    //           $("#parts2").append(str);
    //           //删除父级元素
    //       $(".shanchu").click(function() {
    //           $(this).parent().parent().remove();
    //       });
    //  });

    //按钮先执行验证再提交表单
    $(document).ready(function () {
        /* AJAX选择品牌 */
        $("#ajax_brand").brandinit();

        $('#dssubmit').click(function () {
            $('#formSearch').submit();
        });
        $('#dssubmit').click(function () {
            $('#formSearch').submit();
        });
        $('#company_address').ds_region();
    });
    $(function () {

        //上传图片
        $(document).on('change', '#videos', function () {

            // $('#dataLoading').show();
            var files = document.getElementById('videos').files[0];

            var formData = new FormData();
            var obj = $(this);
            formData.append('file', files);
            var url = $(this).attr('data-action');
            $.ajax({
                //请求方式
                type: "POST",
                //请求地址
                url: url,
                data: formData,
                cache: false,  //默认是true，但是一般不做缓存
                processData: false, //用于对data参数进行序列化处理，这里必须false；如果是true，就会将FormData转换为String类型
                contentType: false,  //一些文件上传http协议的关系，自行百度，如果上传的有文件，那么只能设置为false
                //请求成功
                success: function (result) {

                    if (result.msg == 1) {
                        $('.vide').empty();
                        $('#dataLoading').hide();
                        layer.msg('上传成功');
                        $("#video_name").val(result.files_name);
                        // $("type[name='old_filename']").val(result.old_name);
                    } else {
                        obj.val('');
                        $('#dataLoading').hide();
                        layer.msg('文件信息错误,请重新上传');
                        return false;
                    }

                },
                //请求失败，包含具体的错误信息
                error: function (e) {
                    console.log(e.status);
                    console.log(e.responseText);
                }
            });

        });


        // 类型搜索
        $(document).off('change', "#gcategory > select").on("change", "#gcategory > select", function () {
            type_scroll($(this));
        });
        var typeScroll = 0;
        function type_scroll(o) {
            var id = o.val();
            if (!$('#type_dt_' + id).is('dt')) {
                return false;
            }
            $('#type_div').scrollTop(-typeScroll);
            var sp_top = $('#type_dt_' + id).offset().top;
            var div_top = $('#type_div').offset().top;
            $('#type_div').scrollTop(sp_top - div_top);
            typeScroll = sp_top - div_top;
        }
        gcategoryInit('gcategory');


        $('#area_id1').change(function () {
            var htmls = '<option value="0">请选择...</option>';
            $('#area_id2').html(htmls);
            $('#area_id3').html(htmls);
            var area_id = $(this).val();
            if (area_id == '0') {
                var htmls = '<option value="0">请选择...</option>';
                $('#area_id2').html(htmls);
                $('#area_id3').html(htmls);
            } else {
                $.ajax({
                    //请求方式
                    type: "POST",
                    //请求地址
                    url: "ajax_get_area_class",
                    //数据，json字符串
                    data: { 'area_id': area_id },
                    //请求成功
                    success: function (result) {
                        var htmls = '<option value="0">请选择...</option>';
                        $.each(result, function (item, v) {
                            htmls += "<option value='" + v.area_id + "'>" + v.area_name + "</option>";
                        });

                        $('#area_id2').html(htmls);
                    },
                    //请求失败，包含具体的错误信息
                    error: function (e) {
                        console.log(e.status);
                        console.log(e.responseText);
                    }
                });
            }
        });

        $('#area_id2').change(function () {
            var htmls = '<option value="0">请选择...</option>';
            var area_id = $(this).val();

            $('#area_id3').html(htmls);
            if (area_id == '0') {

                /*  $('#gc_id2').html(htmls);*/
                $('#area_id3').html(htmls);
            } else {
                $.ajax({
                    //请求方式
                    type: "POST",
                    //请求地址
                    url: "ajax_get_area_class",
                    //数据，json字符串
                    data: { 'area_id': area_id },
                    //请求成功
                    success: function (result) {
                        var htmls = '<option value="0">请选择...</option>';
                        $.each(result, function (item, v) {
                            htmls += "<option value='" + v.area_id + "'>" + v.area_name + "</option>";
                        });

                        $('#area_id3').html(htmls);
                    },
                    //请求失败，包含具体的错误信息
                    error: function (e) {
                        console.log(e.status);
                        console.log(e.responseText);
                    }
                });
            }
        });
    });


    function confirm() {
        var _area_3 = $('#_area_3').val();
        var class_id = $('#class_id').val();
        var lastId = $('#upload_img').parent().next().find('span').last().attr('data_id');

        if (_area_3 == '') {
            layer.msg('请选择所在地'); return false;
        }
        if (class_id == '0') {
            layer.msg('请选择分类'); return false;
        }
        if (lastId == '-1') {
            layer.msg('请上传图片'); return false;
        }
        var res = $('form').valid();
        if (res) {
            var url = $('form').attr('action');
            //alert(url);return false;
            // alert(url);return false;
            $.ajax({
                type: "POST",
                url: url,
                data: $('#add_form').serialize(),// 你的formid

                error: function (request) {
                    alert("Connection error");
                },
                success: function (data) {
                    if (data.code == 0) {

                        layer.msg(data.msg);
                        location.href = data.url; return false;
                    } else {
                        layer.msg(data.msg);
                        location.href = data.url; return false;

                    }
                }
            }); return false;
        } else {
            layer.msg('操作失败'); return false;
        }
    }

    //旧的
    function ischild() {
        $('.add-answer').each(function () {
            var ischild = $(this).parent().parent().next().attr('ischild');
            if (ischild) {
                $(this).css('background', '#ccc');
                $(this).attr('disabled', true)
            } else {
                $(this).css('background', '');
                $(this).removeAttr('disabled')
            }
        })
        $('button').each(function () {
            if ($(this).text() == '添加原因') {
                var ischild = $(this).parent().parent().next().attr('ischild');
                if (ischild) {
                    $(this).css('background', '#ccc');
                    $(this).attr('disabled', true)
                } else {
                    $(this).css('background', '');
                    $(this).removeAttr('disabled')
                }
            }

        })
    }
    ischild()

    $('#btn').click(function () {
        if (!$('#gc_name').val()) {
            layer.msg('设备名称不能为空');
            return false;
        }
        var class_id = $('#class_id').val();
        if (class_id == 0) {
            layer.msg('请选择分类');
            return false;
        }
        var b_id = $('#b_id').val();
        if (b_id == 0) {
            layer.msg('请选择品牌');
            return false;
        }
        var question = $('#question').val();
        if (question == 0) {
            layer.msg('请选择绑定的问题');
            return false;
        }
        // if (!$('#specifications').val()) {
        //     layer.msg('规格不能为空');
        //     return false;
        // }
        // if (!$('#type_model').val()) {
        //     layer.msg('型号不能为空');
        //     return false;
        // }
        $('#question_class_form').submit();
    })
    $('#title').blur(function () {
        if ($(this).attr('is_add') == 0) {
            return false;
        }
        if (!$(this).val()) {
            layer.msg('请输入问题名称')
            return false;
        }
        $.post('uniqueQuestion', {
            title: $(this).val()
        }, function (res) {
            if (res.code == 10000) {
                layer.msg(res.message);
                $(this).text('')
                $(this).val('')
                $('#btn').css('background', '#ccc');
                $('#btn').attr('disabled', 'true');
            } else {
                $('#btn').css('background', '');
                $('#btn').removeAttr('disabled');
            }
        }, 'json')
    })
    function EditAnswerPost(that) {
        var id = that.attr('data-id');
        var pass = $('#answer-content').val()
        var method = $('#method-content').val()
        if (!pass) {
            layer.msg('原因不能为空');
            return false;
        }
        if (!method) {
            layer.msg('处理方法不能为空');
            return false;
        }
        $.post('addMethod.html?id=' + id, {
            content: pass,
            method: method
        }, function (res) {
            console.log(res)
            layer.msg(res.message)
            if (res.code == 10000) {
                console.log(213123);
                $('.content-text-' + id).text(res.data.content)
                $('.method-text-' + id).text(res.data.method)
            }
            layer.closeAll()
        }, 'json')
    }
    // TODO 添加或修改处理方法
    function addMethod(that) {
        $('#add-answer-zidi').hide();
        $('#edit-answer-zidi').show();
        var id = that.attr('data-id');
        var content = $('.content-text-' + id).text();
        var method = $('.method-text-' + id).text();

        $('#answer-content').val($('.content-text-' + id).text());
        $('#method-content').val(method)
        layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            shadeClose: true,
            skin: 'yourclass',
            offset: ['8%', '40%'],
            content: $('#add-answer-textaret')
        });
        $('#edit-answer-zidi').attr('data-id', id)
        $('#edit-answer-zidi').attr('data-that', $('#edit-answer-' + id))
    }
    // TODO 修改选项
    function updateOption(that) {
        var id = that.attr('data-id');
        var content = that.attr('data-option');
        var index = layer.prompt({ title: '修改-' + content, formType: 2, value: content }, function (pass, index) {
            layer.close(index);
            $.post('updateOption.html?id=' + id, {
                content: pass
            }, function (res) {
                layer.msg(res.message);
                if (res.code == 10000) {
                    that.parent().children().eq(0).text(pass)
                }
            }, 'json')
            // layer.msg('添加选项“'+pass+'"成功')
        });
    }
    // TODO 点击新增下级
    function addChild(that) {
        var parentid = that.attr('data-id'); // 父级的id
        var lev = that.attr('data-lev'); // 父级的级别
        layer.prompt({ title: '请输入次级现象', formType: 2 }, function (pass, index) {
            layer.close(index);;
            addOption(pass, parentid, parseInt(lev) + 1)
            // layer.msg('添加选项“'+pass+'"成功')
        });
    }

    // TODO 增加答案
    function addAnswer(that) {
        $('#answer-content').val('');
        $('#method-content').val('');
        var parentid = that.attr('data-parentid')
        var id = that.attr('data-id');
        var lev = that.attr('data-lev')
        console.log(id);
        layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            shadeClose: true,
            skin: 'yourclass',
            offset: ['8%', '40%'],
            content: $('#add-answer-textaret')
        });
        $('#add-answer-zidi').show();
        $('#edit-answer-zidi').hide();
        $('#add-answer-zidi').attr('data-id', id)
        $('#add-answer-zidi').attr('data-lev', lev)
        $('#add-answer-zidi').attr('data-that', $('#add-answer-' + id))
        $('#add-answer-zidi').attr('data-parentid', parentid)
    }
    var isclose = false;
    function addAnswerPost(that) {

        var id = that.attr('data-id');
        var lev = that.attr('data-lev')
        var pass = $('#answer-content').val();
        var method = $('#method-content').val();
        var thoese = $('add-answer-' + id);
        var parentid = that.attr('data-parentid');
        if (!pass) {
            layer.msg('原因不能为空');
            return false;
        }
        if (!method) {
            layer.msg('处理方法不能为空');
            return false;
        }
        console.log('----');
        console.log(id)
        var rfalse = false;
        $('.ss').each(function () {
            console.log($(this).text())
            if ($(this).text() == pass) {
                rfalse = true;
                layer.msg('该原因已存在');
                return false;
            }
        })
        $('.mm').each(function () {
            console.log($(this).text())
            if ($(this).text() == method) {
                rfalse = true;
                layer.msg('该处理办法已存在');
                return false;
            }
        })
        if (rfalse) {
            layer.msg('该原因已存在')
            return false;
        }
        if (isclose) {
            layer.msg('点击过快，请稍后。。')
            return false;
        }
        isclose = true;
        $.post('addOptionAnswer?option_id=' + id, {
            content: pass,
            method: method,
            delmethodid: $('#del_all_answer').val()
        }, function (res) {
            if (res.code != 10000) {
                layer.msg(res.message, { time: 500 }, function () {
                    // layer.closeAll()
                    isclose = false
                })
                return false;
            }
            var pas = '';
            for (i = 0; i < lev; i++) {
                pas += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
            }
            pas += '--';
            console.log(res.data.id)
            var html = '<tr class="noborder answer_' + res.data.id + ' option_' + id + ' parent_id_' + parentid + '" ischild="1">\n' +
                '<td class="required"></td>' +
                '<td class="vatop rowform">\n' +
                '    ' + pas + '<span class="ss content-text-' + res.data.id + ' zy">' + pass + '</span> <button type="button" data-content="' + pass + '" data-option-id="' + id + '" data-id="' + res.data.id + '" onclick="deleteAnswer($(this))" class="btn btn-small layui-btn-danger">删除</button>\n' +
                '&nbsp;<button type="button" data-content="' + pass + '" data-method="' + res.data.method + '" data-id="' + res.data.id + '" onclick="addMethod($(this))" class="btn btn-small">修改</button>' +
                '<div><br>' +
                '   ' + pas + '<span class="mm method-text-' + res.data.id + '">' + res.data.method + '</span>\n' +
                '\n' +
                '                    </div>'
            '</td>\n' +
                '</tr>';
            // console.log(html)
            $('#add-answer-' + id).parent().parent().after(html);
            var answer_ids = $('#option_answer_' + id).val();
            console.log(answer_ids);
            console.log('#option_answer_' + id);
            console.log(answer_ids + ',' + res.data.id);
            if (!answer_ids) {
                $('#add-answer-' + id).next().val(res.data.id);
            } else {
                $('#add-answer-' + id).next().val(answer_ids + ',' + res.data.id);
            }
            $('#add-answer-' + id).attr('disabled', true)
            $('#add-answer-' + id).css('background', '#ccc');

            that.attr('data-id', '');
            that.attr('data-lev', '')
            $('#answer-content').val('');
            $('#method-content').val('');
            layer.msg(res.message, { time: 1000 }, function () {
                layer.closeAll()
                isclose = false
            })

        });
        ischild()
    }

    // TODO 删除答案
    function deleteAnswer(that) {
        var id = that.attr('data-id');
        var option_id = that.attr('data-option-id');
        var index = layer.confirm('确定要删除"' + that.attr('data-content') + '"答案吗？', {
            btn: ['确定', '取消'],
            title: false,
        }, function () {
            $('.answer_' + id).remove();
            ischild()
            var del = $('#option_answer_del_' + option_id).val();
            $('#option_answer_del_' + option_id).val(del + ',' + id);
            $('#del_all_answer').val(del + ',' + id);
            var answer_id = $('#option_answer_' + option_id).val();
            if (!answer_id) {
                answer_id = '';
            }
            answer_id = answer_id.split(',');
            for (i in answer_id) {
                if (!answer_id[i]) {
                    answer_id.splice(i, 1); // 删除空元素
                }
                if (answer_id[i] == id) {
                    answer_id.splice(i, 1); // 删除删掉的答案id
                }
            }
            answer_id = answer_id.join(',');
            if (!answer_id) {
                $('#option_answer_' + option_id).prev().removeAttr('disabled')
                $('#option_answer_' + option_id).prev().removeClass('background')
                $('#option_answer_' + option_id).prev().css('background', '')
            }
            $('#option_answer_' + option_id).val(answer_id);
            layer.msg('删除成功', { time: 500 }, function () {
                ischild()
                layer.close(index)
            })


        })
        ischild()
    }

    // TODO 新增环境配置
    $('#add-science').click(function () {
        var science = $('#science').val();
        addScience(science, 0, 0)
        ischild()
    })

    /**
     *  TODO 环境配置
     * @param option
     * @returns {boolean}
     */
    function addScience(science, parentid, lev) {
        if (!science) {
            layer.msg('文本不能为空');
            return false;
        }
        if (!parentid) parentid = 0;
        if (!lev) lev = 0;
        var isfalse = false;
        $('.thisoption').each(function () {
            if ($(this).text() == option) {
                isfalse = true;
            }
        })
        // if (isfalse) {
        //     layer.msg('已存在该现象');
        //     return false;
        // }
        $.post('addScience?parent_id=' + parentid, {
            science: science,
            lev: lev,
            deloption: $('#del_all_option').val()
        }, function (res) {

            console.log(res);
            layer.msg(res.message);
            if (res.code == 10000) {
                var del = $('#del_all_option').val()
                console.log('--+' + del)
                del = del.split(',');
                for (i in del) {
                    if (del[i] == res.data.id) {
                        del.splice(i, 1)
                    }
                }
                del = del.join(',');
                console.log('--' + del)
                $('#del_all_option').val(del)
                var lev_html = '';
                var str = '<tr>';
                str += '<td colspan="2" class="required"><label style="margin-left: 46px;"></label>';
                str += '<div style="width:230px;margin-left: 117px;">';
                str += '<input type="hidden" value="' + res.data.id + '" name="science[]" style="width: 200px; margin-left: 75px;" class="txt"><span></span>';
                str += '<span class="thisoption" style="width:100px;">' + science + '</span>';
                str += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                str += '<button type="button" class=" btn btn-small layui-btn-danger shanchu" style="background-color: #FF5722;">删除</button></td>';
                str += '</div>';
                str += '</tr>';

                if (parentid != 0) {
                    $('.science_' + parentid).after(str);
                } else {
                    $("#parts2").append(str);
                }
                $('#science').val(option);
                $('#science').val('');

                var science_ids = $('#science-hidden').val();
                var science_arr = science_ids.split(',');
                science_arr.push(res.data.id);
                science_ids = science_arr.join(',');
                $('#science-hidden').val(science_ids);

                $(".shanchu").click(function () {
                    $(this).parent().parent().remove();
                });
            }
        }, 'json')
        ischild()
    }

    // TODO 点击添加选项
    $('#add-option').click(function () {
        var option = $('#option').val();
        var an_option = $('#an_option').val();
        var wei_option = $('#wei_option').val();
        addOption(option, an_option, wei_option, 0, 0)
        ischild()
    })

    /**
     *  TODO 添加选项
     * @param option
     * @returns {boolean}
     */
    function addOption(option, an_option, wei_option, parentid, lev) {
        if (!option) {
            layer.msg('部位不能为空');
            return false;
        }
        if (!parentid) parentid = 0;
        if (!lev) lev = 0;
        var isfalse = false;
        $('.thisoption').each(function () {
            if ($(this).text() == option) {
                isfalse = true;
            }
        })
        // if (isfalse) {
        //     layer.msg('已存在该现象');
        //     return false;
        // }
        $.post('addOption?parent_id=' + parentid, {
            option: option,
            an_option: an_option,
            wei_option: wei_option,
            lev: lev,
            deloption: $('#del_all_option').val()
        }, function (res) {

            console.log(res);
            layer.msg(res.message);
            if (res.code == 10000) {
                var del = $('#del_all_option').val()
                console.log('--+' + del)
                del = del.split(',');
                for (i in del) {
                    if (del[i] == res.data.id) {
                        del.splice(i, 1)
                    }
                }
                del = del.join(',');
                console.log('--' + del)
                $('#del_all_option').val(del)
                var lev_html = '';
                // if (lev > 0) {
                //     for (i = 0; i < lev; i ++) {
                //         lev_html += '&nbsp;&nbsp;&nbsp;&nbsp;|__';
                //     }
                // }
                // var html = 
                //     '<tr class="noborder option_'+res.data.id+' parent_id_'+parentid+'">\n' +
                //     '<td class="required"></td>'+
                //     '<td class="vatop rowform">\n' +
                //     ''+lev_html+' <span class="thisoption">'+option+'</span>'+
                //     ''+lev_html+' <span class="thisoption">'+an_option+'</span>'+
                //     ''+lev_html+' <span class="thisoption">'+wei_option+'</span>'+
                //      '<button type="button" data-option="'+option+'" data-id="'+res.data.id+'" onclick="deleteOption($(this))" class="btn btn-small btn-red layui-btn-danger del-option">删除</button>\n' +
                //     '<button type="button" data-option="'+option+'" onclick="updateOption($(this))" data-id="'+res.data.id+'" class="btn btn-small layui-btn-danger">修改&nbsp;</button>';

                //     if (lev != 0) {
                //         html += '&nbsp;<button type="button" data-option="'+option+'" data-lev="'+res.data.lev+'" data-id="'+res.data.id+'" id="add-answer-'+res.data.id+'" data-parentid="'+parentid+'" onclick="addAnswer($(this))" class="btn btn-small layui-btn-danger add-answer">添加原因</button>';
                //     }
                //     html += '<input type="hidden" name="option_answer['+res.data.id+']" id="option_answer_'+res.data.id+'}">\n' +
                //     '<input type="hidden" name="option_answer_del[]" id="option_answer_del_'+res.data.id+'">'
                //     '                </td>\n' +
                //     '            </tr>';
                var html =
                    '<tr class="noborder option_' + res.data.id + ' parent_id_' + parentid + '">\n' +
                    '<td class="required"></td>' +
                    '<td class="vatop rowform">\n' +
                    '部位:' + lev_html + ' <span class="thisoption">' + option + '</span>' +
                    '&nbsp;保养工时:' + lev_html + ' <span class="thisoption">' + an_option + '</span>' +
                    '&nbsp;维修工时:' + lev_html + ' <span class="thisoption">' + wei_option + '</span>' +
                    '&nbsp;&nbsp;&nbsp;' + lev_html + '<button type="button" data-option="' + option + '" data-id="' + res.data.id + '" onclick="deleteOption($(this))" class="btn btn-small btn-red layui-btn-danger del-option">删除</button>\n';

                // if (lev != 0) {
                //     html += '&nbsp;<button type="button" data-option="'+option+'" data-lev="'+res.data.lev+'" data-id="'+res.data.id+'" id="add-answer-'+res.data.id+'" data-parentid="'+parentid+'" onclick="addAnswer($(this))" class="btn btn-small layui-btn-danger add-answer">添加原因</button>';
                // }
                html += '<input type="hidden" name="option_answer[' + res.data.id + ']" id="option_answer_' + res.data.id + '}">\n' +
                    '<input type="hidden" name="option_answer_del[]" id="option_answer_del_' + res.data.id + '">'
                '                </td>\n' +
                    '            </tr>';
                if (parentid != 0) {
                    $('.option_' + parentid).after(html);
                } else {
                    $('tbody').append(html);
                }
                $('#option').val(option);
                $('#an_option').val(an_option);
                $('#wei_option').val(wei_option);
                $('#option').val('');
                $('#an_option').val('');
                $('#wei_option').val('');
                var option_ids = $('#option-hidden').val();
                var option_arr = option_ids.split(',');
                option_arr.push(res.data.id);
                option_ids = option_arr.join(',');
                $('#option-hidden').val(option_ids);
            }
        }, 'json')
        ischild()
    }

    // TODO 删除选项
    function deleteOption(that) {
        var index = layer.confirm('确定要删除"' + that.attr('data-option') + '"的部位吗', {
            btn: ['确定', '取消'],
            title: false,
        }, function () {
            var id = that.attr('data-id');
            $('.option_' + id).remove();
            $('.parent_id_' + id).remove();
            var option_ids = $('#option-hidden').val();
            var option_arr = option_ids.split(',');
            for (i in option_arr) {
                if (option_arr[i] == id) {
                    option_arr.splice(i, 1)
                }
            }
            var option = option_arr.join(',');
            $('#option-hidden').val(option);
            $('#del_all_option').val($('#del_all_option').val() + ',' + id)
            layer.msg('删除成功');
            layer.close(index)
        });
        ischild()
    }

    function getGcId1(gc_id1, select_id) {
        var select_od = select_id;
        $.ajax({
            type: "GET",
            contentType: "application/json;charset=UTF-8",
            url: ADMINSITEURL + "/Question/getClass.html?gc_id=" + gc_id1,
            success: function (result) {
                result = JSON.parse(result);
                var html = '';
                for (var i = 0; i < result.length; i++) {
                    if (result[i].gc_id == select_id) {
                        html += '<option selected value="' + result[i].gc_id + '">' + result[i].gc_name + '</option>'
                    } else {
                        html += '<option value="' + result[i].gc_id + '">' + result[i].gc_name + '</option>'
                    }
                }
                $('#gc_id2').html(html)
            },
            //请求失败，包含具体的错误信息
            error: function (e) {
                console.log(e.status);
                console.log(e.responseText);
            }
        });
    }

    function getGcId2(gc_id1, select_id) {
        $.ajax({
            type: "GET",
            contentType: "application/json;charset=UTF-8",
            url: ADMINSITEURL + "/Question/getClass.html?gc_id=" + gc_id1,
            success: function (result) {
                result = JSON.parse(result);
                var html = '';
                for (var i = 0; i < result.length; i++) {
                    if (result[i].gc_id == select_id) {
                        html += '<option selected value="' + result[i].gc_id + '">' + result[i].gc_name + '</option>'
                    } else {
                        html += '<option value="' + result[i].gc_id + '">' + result[i].gc_name + '</option>'
                    }
                }
                $('#gc_id3').html(html)
            },
            //请求失败，包含具体的错误信息
            error: function (e) {
                console.log(e.status);
                console.log(e.responseText);
            }
        });
    }

    // 获取下级分类
    $('#gc_id1').on('change', function () {
        var gc_id1 = $(this).val();
        getGcId1(gc_id1)
    })

    $('#gc_id2').on('change', function () {
        var gc_id2 = $(this).val();
        getGcId2(gc_id2)
    })


    $('.del-option').click(function () {
        deleteOption($(this));

    })
</script>
{if isset($model)}
<script>

    // 编辑初始化，处理分类选中的问题
    if ($('#gc_id1').val()) {
        var gc_id1 = '{$model->gc_id1}';
        var gc_id2 = '{$model->gc_id2}';
        var gc_id3 = '{$model->gc_id3}';
        getGcId1(gc_id1, gc_id2)
        getGcId2(gc_id2, gc_id3)
    }
</script>
{/if}